<?php
// +----------------------------------------------------------------------
// | title: 标题注释
// +----------------------------------------------------------------------
// | Author: TZ.Leong
// +----------------------------------------------------------------------
// | Date: 2019/8/3 17:56
// +----------------------------------------------------------------------

namespace app\single\api;


use think\Db;
use think\facade\Cache;
use think\facade\Log;

class Synchro
{
    public function run($params)
    {
        $ret["ret"] = 1;
        $ret["data"] = [];
        $data = json_decode(base64_decode($params["data"]), true);
        $type = $data["sync_type"];
        $operType = $data["oper_type"];
        unset($data["sync_type"], $data["oper_type"]);
        switch ($type) {
            //同步群组
            case 1:
                switch ($operType) {
                    //添加群组
                    case 1:
                        $authAccess = $data["auth_access"];
                        unset($data["auth_access"]);
                        $checkId = Db::name("role")->where("id", $data["id"])->value("id");
                        if ($checkId > 0) {
                            Db::name("role")->update($data);
                        } else {
                            Db::name("role")->insert($data);
                        }
                        if ($authAccess) {
                            if (false !== Db::name("auth_access")->where("role_id", $data["id"])->delete()) {
                                Db::name("auth_access")->insertAll($authAccess);
                            }
                        }
                        break;
                    //删除群组
                    case 3:
                        if (isset($data["id"]) && $data["id"] > 0) {
                            if (false !== Db::name("role")->where("id", $data["id"])->delete()) {
                                Db::name("auth_access")->where("role_id", $data["id"])->delete();
                            }
                        }
                        break;
                }
                //清掉菜单缓存
                Cache::clear("admin_menus");
                break;
            //同步管理员
            case 3:
                switch ($operType) {
                    //添加管理员
                    case 1:
                        $roleUser = $data["role_user"];
                        unset($data["role_user"], $data["id"]);
                        $roleId = Db::name("user")->where("user_login", $data["user_login"])->value("id");
                        Log::write("debug roleid:".$roleId);
                        if ($roleId) {
                            Db::name("user")->where("id", $roleId)->update($data);
                        } else {
                            $roleId = Db::name("user")->insertGetId($data);
                        }
                        if ($roleUser) {
                            foreach ($roleUser as &$vo) {
                                $vo["user_id"] = $roleId;
                            }
                            if (false !== Db::name("roleUser")->where("user_id", $roleId)->delete()) {
                                Db::name("roleUser")->insertAll($roleUser);
                            }
                        }
                        break;
                    //删除管理员
                    case 3:
                        if (isset($data["user_login"]) && $data["user_login"]) {
                            $checkId = Db::name("user")->where("user_login", $data["user_login"])->value("id");
                            if (false !== Db::name("user")->where("id", $checkId)->delete()) {
                                Db::name("roleUser")->where("user_id", $checkId)->delete();
                            }
                        }
                        break;
                }
                break;
        }
        return $ret;
    }
}